package com.ztgf.shopping.service.shop;

import com.ztgf.shopping.models.dto.shop.ShopAnnualFeeDTO;
import com.ztgf.shopping.models.dto.shop.ShopInfoDTO;
import io.netty.util.Timeout;
import io.netty.util.Timer;
import io.netty.util.TimerTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: mxli
 * @Date: 2021-04-21 09:42
 **/
@Slf4j
@DubboService

public class AnnualFeeTimerTaskServiceImpl implements AnnualFeeTimerTaskService{

    @Autowired
    @Lazy
    private ShopInfoServiceImpl shopInfoService;
    @Autowired
    private ShopAnnualFeeServiceImpl shopAnnualFeeService;

    @Autowired
    private Timer timer;

    private HashMap<String, Timeout> hashMap = new HashMap<>();

    public Map<String, Timeout> getHashMap() {
        return hashMap;
    }

    public void remove(String feeId){
        Timeout timeout = hashMap.get(feeId);
        if (timeout != null){
            timeout.cancel();//停止这个时间轮
            hashMap.remove(feeId + "");//map中去除这条时间轮
        }
    }

    /**
     * 添加进时间轮
     *
     * @param time    定时时间(天)
     */
    public void createOrderCountDown(int time, String feeId) {
        remove(feeId);
        TimerTask timerTask = new DefaultTimerTask(feeId);
        Timeout timeout = timer.newTimeout(timerTask, time, TimeUnit.DAYS);
        hashMap.put(feeId, timeout);
    }


    private class DefaultTimerTask implements TimerTask {
        private final DateTimeFormatter F = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        private String feeId;
        private long timestamp;

        public DefaultTimerTask(String feeId) {
            this.feeId = feeId;
            this.timestamp = System.currentTimeMillis();
        }

        @Override
        public void run(Timeout timeout){
            log.info(String.format("任务执行时间:%s,年费异常时间:%s 年费ID:%s",
                    LocalDateTime.now().format(F), LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).format(F), feeId));

            ShopAnnualFeeDTO shopAnnualFeeDTO = shopAnnualFeeService.selectById(Long.parseLong(feeId));

            ShopAnnualFeeDTO newdto = new ShopAnnualFeeDTO();
            newdto.setId(shopAnnualFeeDTO.getId());
            newdto.setCountdownStatus(2);//倒计时状态 0未启动，1启动中，2已过期
            shopAnnualFeeService.updateById(newdto);

            //冻结店铺
            ShopInfoDTO shopInfoDTO = new ShopInfoDTO();
            shopInfoDTO.setId(shopAnnualFeeDTO.getShopId());
            shopInfoDTO.setFrozenStatus(1);//冻结状态 0未冻结，1冻结 2关店
            shopInfoService.updateById(shopInfoDTO);

            hashMap.remove(feeId);//map中去除这条时间轮
        }
    }
}
